package com.shenzhen.horselive.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.shenzhen.horselive.R;

public class LimitEditetView extends LinearLayout {
    private  int  length ;
    private  String  hint;
    private  int  graty;

    //设置字数限制
    public void setLength(int length) {
        this.length = length;
    }

    public LimitEditetView(Context context) {
        super(context);
        initviews();
    }

    EditTextWithScrollView  editText;
    TextView  textView;
    private void initviews() {
        inflate(getContext(), R.layout.view_limit_edittext,this);
        editText=findViewById(R.id.et_input);
        textView =findViewById(R.id.tv_number);
        if (length==0)
        {
            textView.setVisibility(GONE);
        }else
        {
            textView.setVisibility(VISIBLE);
        }
        textView.setText("0/"+length);
        editText.setHint(hint);
        editText.setGravity(graty);
         editText.addTextChangedListener(new TextWatcher() {
             private CharSequence temp;
             private int selectionStart;
             private int selectionEnd;
             @Override
             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                   temp = s;
             }

             @Override
             public void onTextChanged(CharSequence s, int start, int before, int count) {

             }

             @Override
             public void afterTextChanged(Editable s) {
                 if (length!=0)
                 {
                 int number =s.length();
                 textView.setText("" + number+"/"+length);
                 selectionStart = editText.getSelectionStart();
                 selectionEnd = editText.getSelectionEnd();
                 //System.out.println("start="+selectionStart+",end="+selectionEnd);
                 if (temp.length() > length) {
                     s.delete(selectionStart - 1, selectionEnd);
                     int tempSelection = selectionStart;
                     editText.setText(s);
                     editText.setSelection(tempSelection);//设置光标在最后
                 }
                 }

             }
         });



        // 复写EditText的onTouch方法
//        editText.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                // 当触摸的是EditText & 当前EditText可滚动时，则将事件交给EditText处理；
//                if ((v.getId() == R.id.et_input && canVerticalScroll(editText))) {
//                    v.getParent().requestDisallowInterceptTouchEvent(true);
//                    // 否则将事件交由其父类处理
//                    if (event.getAction() == MotionEvent.ACTION_UP) {
//                        v.getParent().requestDisallowInterceptTouchEvent(false);
//                    }
//                }
//                return false;
//            }
//        });

    }


    public String  getText()
    {
        return editText.getText().toString();
    }

    public LimitEditetView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.LimitEditetView);
        length = array.getInteger(R.styleable.LimitEditetView_number,0);
        hint  =array.getString(R.styleable.LimitEditetView_hint);
        graty  = array.getInteger(R.styleable.LimitEditetView_mygravity, Gravity.START);
        array.recycle();
        initviews();

    }


    // 判断当前EditText是否可滚动
    private boolean canVerticalScroll(EditText editText) {

        if (editText.getLineCount() > editText.getMaxLines()) {
            return true;
        }
        return false;
    }
}
